package com.cy.jxc.controller;

import com.cy.jxc.entity.Client;
import com.cy.jxc.service.ClientService;
import com.cy.jxc.vo.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/client/")
public class ClientController {
    @Autowired
    ClientService clientService;
    @RequestMapping("client")
    public JsonResult doFindClient(){
        List<Client> rows = clientService.findClientObject();
        return new JsonResult(rows);
    }
    @RequestMapping("doFindClientByPage")
    public JsonResult doFindClientByPage(Integer pageCurrent){
        Integer rows=6;
        JsonResult jsonresult = clientService.findClientByPage(pageCurrent, rows);
        return jsonresult;
    }

    @RequestMapping("doSelectClientById")
    public JsonResult doFindClientById(Integer clientid){
        Client client=clientService.findClientById(clientid);
        if(client!=null){
            return new JsonResult(client);
        }else {
            return new JsonResult("没有查找到相应的信息");
        }
    }

    @RequestMapping("doDeleteClientById")
    public JsonResult doDeleteClientById(Integer clientid){
        Integer rows=clientService.deleteClientById(clientid);
        if(rows!=null&&rows!=0){
            return new JsonResult("删除成功");
        }else {
            return new JsonResult("删除失败");
        }
    }


    @GetMapping("doSelectClientByName")
    public JsonResult doFindClientByName(String clientname){

        List<Client> rows = clientService.findClientByname(clientname);
        return new JsonResult(rows);
    }


    @PostMapping("doSaveClient")
    public JsonResult doSaveClient(Client entity){
        Integer rows = clientService.insertClient(entity);
        if(rows==0){
            throw new RuntimeException("添加失败");
        }else{
            return new JsonResult("添加成功");
        }
    }
    @PostMapping("doUpdateClient")
    public JsonResult doUpdateClient(Client entity){
        System.out.println(entity);
        int rows =clientService.updateClient(entity);
        if(rows==0){
            throw new RuntimeException("更新失败,客户姓名填写不正确");
        }else{
            return new JsonResult("更新成功");
        }
    }
}
